package com.why.serviceedu.controller;


import com.why.commonresults.Result;
import com.why.serviceedu.bean.EduCourse;
import com.why.serviceedu.bean.Vo.courseInfoVo;
import com.why.serviceedu.service.EduCourseService;
import com.why.serviceedu.bean.Vo.coursePublishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author why
 * @since 2021-02-01
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService course;

    /**
     * 查询所有的课程信息 // TODO: 2021/2/5
     *
     * @return
     */
    @GetMapping("/findAllCourse")
    public Result getAllCourseInfo() {
        List<EduCourse> courses = course.list(null);
        return Result.ok().data("courses", courses);
    }


    @PostMapping("/addCourseInfo")
    public Result addCourseInfo(@RequestBody courseInfoVo courseVo) {
        String id = course.saveVo(courseVo);


        return Result.ok().data("id", id);

    }

    @GetMapping("/getCourseById/{courseId}")
    public Result getCourseById(@PathVariable String courseId) {

        courseInfoVo courseInfoVo = course.getCourseVoById(courseId);
        return Result.ok().data("course", courseInfoVo);

    }

    @PostMapping("/updateCourseById")
    public Result updateCourseById(@RequestBody courseInfoVo courseInfo) {

        course.updateCourseById(courseInfo);

        return Result.ok();
    }


    @GetMapping("/getPublishCourseById/{courseId}")
    public Result getPublishCourseById(@PathVariable String courseId) {

        coursePublishVo course = this.course.publishCourseInfo(courseId);

        return Result.ok().data("course", course);
    }


    @PostMapping("/updateCourseStatus/{courseId}")
    public Result updateCourseStatus(@PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        course.updateById(eduCourse);

        return Result.ok();
    }


    /**
     * 删除课程
     */
    @PostMapping("/deleteCourse/{courseId}")
    public Result deleteCourseById(@PathVariable String courseId) {
        course.removeCourseInfoById(courseId);

        return Result.ok();
    }






}

